package mvc.config;

import mvc.handlerexception.MyExceptionHandler;
import mvc.myinterceptor.Interceptor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;

/**
 * @author Administrator
 * @author Administrator
 * @author cyt
 * @create 2020-08-15 12:48
 */
@ComponentScan(
        basePackages = {
                "mvc"
        },
        includeFilters = {
                @ComponentScan.Filter(
                        type = FilterType.ANNOTATION,
                        classes = RestController.class
                )
        }
)

/**
 * 开启自定义 spring mvc
 */
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter {
    /**
     * 添加拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new Interceptor()).addPathPatterns("/**");
    }

    /**
     * 试图解析器
     * @param registry
     */
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("", ".jsp");
    }

    /**
     * 试图映射
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        // registry.addViewController().setViewName();
    }

    /**
     * 开始静态资源的访问
     * @param configurer
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    /**
     * 异常处理
     * @param exceptionResolvers
     */
    @Override
    public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
        exceptionResolvers.add(new MyExceptionHandler());
    }
}
